package com.sunwayhorizo.health.business.patad.empi;

import com.sunwayhorizo.health.common.DataTypeUtil;
import com.sunwayhorizo.health.common.DateUtil;
import com.sunwayhorizo.health.common.ListUtil;
import com.sunwayhorizo.health.hl7.common.Hl7Exception;

import java.sql.*;
import java.util.*;
import java.util.Date;

import javax.ejb.*;
import org.jboss.seam.*;
import org.jboss.seam.annotations.*;
import com.sunwayhorizo.health.business.base.*;
import com.sunwayhorizo.health.business.dto.ResultsDTO;
import com.sunwayhorizo.health.business.dto.patad.empi.*;
import com.sunwayhorizo.health.business.dto.registries.*;
import com.sunwayhorizo.health.business.patad.empi.query.PatientQueryLocal;
import com.sunwayhorizo.health.business.registries.employee.*;
import com.sunwayhorizo.health.business.registries.person.*;
import com.sunwayhorizo.health.hl7.datatypes.*;
import com.sunwayhorizo.health.hl7.persistent.*;
import com.sunwayhorizo.health.hl7.rim.*;
import com.sunwayhorizo.health.hl7.rim.Role;
import com.sunwayhorizo.health.hl7.vocabulary.RoleIICode;

/**
 * @Title: ClinicalCardManager
 * @Package com.sunwayhorizo.health.business.registries
 * @Description: 就诊卡登记sessionBean
 * @version V1.0
 * @firstAuthor jinzw
 * @firstTime 2010-12-07 上午09:36:59
 * @lastAuthor jinzw
 * @lastTime 2010-12-07 上午09:36:59
 */

@Name("ClinicalCardManager")
@Stateless
public class ClinicalCardManager extends BaseManager implements ClinicalCardManagerLocal
{

    private PatientQueryLocal patientQuery = (PatientQueryLocal) Component.getInstance("PatientQuery");


    public ResultsDTO newClinicalCard(MedicalCardDTO medicalCardDTO) throws Hl7Exception
    {
        if (medicalCardDTO.getPatientId() != null && !medicalCardDTO.getPatientId().equals(""))
        {
            PatientManagerLocal patientManagerAction = (PatientManagerLocal) Component.getInstance("PatientManager");

            Patient patient = patientQuery.findPatientByNeturalId(medicalCardDTO.getPatientId());

            CE medicalCardType = this.getDataTypeFactory().newCE(medicalCardDTO.getTypeDic().getConceptCode(),
                medicalCardDTO.getTypeDic().getCodingschemeOid(), medicalCardDTO.getTypeDic().getCodingschemeEnName(),
                medicalCardDTO.getTypeDic().getCodingschemeChName(), medicalCardDTO.getTypeDic().getConceptName(),
                medicalCardDTO.getTypeDic().getVersionName());
            II yiBaoID = DataTypeUtil.getII(medicalCardDTO.getId(), medicalCardDTO.getOffice(), medicalCardDTO.getState(), medicalCardType, RoleIICode.MEDICALCARD.toString());
            patient.setId(yiBaoID);

            em.merge(patient);
            dto = new ResultsDTO();
            dto.setResultFlag(true);
            dto.setResultDesc("添加就诊卡成功");
            dto.setResultErrorText(medicalCardDTO.getPatientId() + "患者的" + medicalCardDTO.getId() + "就诊卡添加成功");
            log.info(medicalCardDTO.getPatientId() + "患者的" + medicalCardDTO.getId() + "就诊卡添加成功");
            return dto;
        }
        else
        {
            dto.setResultFlag(false);
            dto.setResultDesc("添加就诊卡失败");
            return dto;
        }

    }


    public ResultsDTO modifyClinicalCard(MedicalCardDTO medicalCardDTO) throws Hl7Exception
    {
        if (medicalCardDTO.getPatientId() != null && !medicalCardDTO.getPatientId().equals(""))
        {
            PatientManagerLocal patientManagerAction = (PatientManagerLocal) Component.getInstance("PatientManager");
            PatientQueryDTO patientQueryDTO = new PatientQueryDTO();
            patientQueryDTO.setPatientId(medicalCardDTO.getPatientId());
            Patient patient = patientQuery.findPatientByMedicalCardId(medicalCardDTO.getPatientId());
            List<RimCoreRoleIi> roleIiList = patient.getRoleIiList();
            if (!roleIiList.isEmpty())
            {
                for (RimCoreRoleIi roleIi : roleIiList)
                {
                    if (roleIi.getExtensionTxt().equals(medicalCardDTO.getId()))
                    {
                        if (medicalCardDTO.getState().equals("退卡"))
                        {
                            log.info("就诊卡状态为退卡时要处理预交金");
                        }
                        roleIi.setIDSouce(medicalCardDTO.getOffice());
                        roleIi.setDisplayableFlag(medicalCardDTO.getState());
                        roleIi.setLastUpdatedBy(Long.valueOf(medicalCardDTO.getModifyOperatorId()));
                        roleIi.setLastUpdateDate(new Timestamp(new Date().getTime()));
                    }
                }
            }
            em.merge(patient);
            dto = new ResultsDTO();
            dto.setResultFlag(true);
            dto.setResultDesc("修改成功");
            dto.setResultErrorText(medicalCardDTO.getPatientId() + "患者的" + medicalCardDTO.getId() + "就诊卡修改成功");
            log.info(medicalCardDTO.getPatientId() + "患者的" + medicalCardDTO.getId() + "就诊卡修改成功");
            return dto;
        }
        else
        {
            dto = new ResultsDTO();
            dto.setResultFlag(false);
            dto.setResultDesc("修改失败");
            return dto;
        }
    }


    public ResultsDTO findClinicalCard(PatientQueryDTO patientQueryDTO) throws Hl7Exception
    {
        PatientManagerLocal patientManagerAction = (PatientManagerLocal) Component.getInstance("PatientManager");
        List<Role> list = new ArrayList<Role>();
        if (ListUtil.isNotEmpty(list))
        {
            EntitysDTO entitysDTO = new EntitysDTO();
            Role role = list.get(0);
            PersonManagerLocal personManager = (PersonManagerLocal) Component.getInstance("PersonManager");
            Person person = (Person) role.getPlayerEntities();
            PatientBaseInfoDTO patientBaseInfo = personManager.constructorPatBaseInfo(person);

            List<MedicalCardDTO> medicalCardList = new ArrayList<MedicalCardDTO>();
            List<RimCoreRoleIi> roleIiList = role.getRoleIiList();
            if (!roleIiList.isEmpty())
            {
                for (RimCoreRoleIi rimCoreRoleIi : roleIiList)
                {
                    if (rimCoreRoleIi.getRimCoreRoleCdSets() != null)
                    {
                        if (rimCoreRoleIi.getRimCoreRoleCdSets().getCodingschemeEnName().equals(""))
                        {
                            MedicalCardDTO medicalCardDTO = new MedicalCardDTO();
                            medicalCardDTO.setId(rimCoreRoleIi.getExtensionTxt());
                            medicalCardDTO.setOffice(rimCoreRoleIi.getIDSouce());
                            medicalCardDTO.setState(rimCoreRoleIi.getDisplayableFlag());
                            medicalCardDTO.setTypeDic(this.getDictionaryFactory().setDictionaryDTO(rimCoreRoleIi.getRimCoreRoleCdSets()));

                            EmployeeManagerLocal emp = (EmployeeManagerLocal) Component.getInstance("EmployeeManager");
                            medicalCardDTO.setOperatorId(String.valueOf(rimCoreRoleIi.getCreatedBy()));
                            PatientBaseInfoDTO patientBaseInfoDTO = emp.findPlayEntityByRoleId(rimCoreRoleIi.getCreatedBy());
                            medicalCardDTO.setOperatorName(patientBaseInfoDTO.getNatureName());
                            medicalCardDTO.setCreateDate(DateUtil.getFormatTimestamp(rimCoreRoleIi.getCreationDate(), "yyyy-MM-dd HH:mm:ss"));

                            medicalCardDTO.setModifyOperatorId(String.valueOf(rimCoreRoleIi.getLastUpdatedBy()));
                            PatientBaseInfoDTO patientBaseInfoDto = emp.findPlayEntityByRoleId(rimCoreRoleIi.getCreatedBy());
                            medicalCardDTO.setModifyDate(DateUtil.getFormatTimestamp(rimCoreRoleIi.getLastUpdateDate(), "yyyy-MM-dd HH:mm:ss"));
                            medicalCardDTO.setModifyOperatorName(patientBaseInfoDto.getNatureName());

                            medicalCardList.add(medicalCardDTO);
                        }
                    }
                    else
                    {
                        // 得到患者ID
                        patientBaseInfo.setNatureId(rimCoreRoleIi.getExtensionTxt());
                    }
                }
            }
            entitysDTO.setListMedicalCard(medicalCardList);
            entitysDTO.setPatientBaseInfo(patientBaseInfo);
            dto = new ResultsDTO();
            dto.setObject(entitysDTO);
            dto.setResultFlag(true);
            dto.setResultDesc("查询成功");
            dto.setResultErrorText("查询出" + entitysDTO.getPatientBaseInfo().getNatureId() + "患者有" + medicalCardList.size() + "个就诊卡");
            log.info("查询出" + entitysDTO.getPatientBaseInfo().getNatureId() + "患者有" + medicalCardList.size() + "个就诊卡");
            return dto;
        }
        else
        {
            dto = new ResultsDTO();
            dto.setResultFlag(true);
            dto.setResultDesc("没有符合条件的记录");
            return dto;
        }
    }

}
